package com.jianyun.jyzs.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.CrmActivity;
import com.jianyun.jyzs.activity.CrmDetialActivity;
import com.jianyun.jyzs.bean.CrmDetial;
import com.jianyun.jyzs.bean.CrmPhoneNumBean;
import com.jianyun.jyzs.bean.CrmSearchBean;
import com.jianyun.jyzs.bean.MeUserInfo;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.widget.PromptDialog;
import com.jrmf360.rylib.common.util.ToastUtil;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.rongcloud.rce.lib.CacheTask;

import com.jianyun.jyzs.utils.RceLog;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by mlli on 2017/8/16.
 * crm的列表适配器
 */

public class CrmAdapter extends RecyclerView.Adapter<CrmAdapter.MyHolder> {
    private CrmActivity context;
    private List<CrmSearchBean.ListCrm> mDatas;
    private List<CrmSearchBean.ListCrm> mDataList = new ArrayList<>();
    private final MeUserInfo userDetaileInfo;


    public CrmAdapter(CrmActivity context, List<CrmSearchBean.ListCrm> list) {
        this.context = context;
        this.mDatas = list;
        userDetaileInfo = LoginCache.getInstance().getUserDetaileInfo();

    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_crm, null);
        return new MyHolder(view);
    }

    public void addList(List<CrmSearchBean.ListCrm> list) {
        mDataList.addAll(list);
        this.mDatas = mDataList;
        notifyDataSetChanged();

    }


    public void clearList() {
        this.mDatas.clear();
        mDataList.clear();
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        final CrmSearchBean.ListCrm bean = mDatas.get(position);
        String iconPaths = userDetaileInfo.getIconPaths();
        String portraitUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl() + iconPaths;
        if (iconPaths != null) {
            Picasso.with(context).load(portraitUrl).into(holder.head);
        } else {
            Picasso.with(context).load(R.drawable.rc_image_error).into(holder.head);
        }
        holder.headName.setText(bean.getSalename());
        holder.tvTitle.setText(bean.getCustomername());
        holder.tvState.setText("状态:" + bean.getLdphase());
        String city = getCityName(bean.getAddress());
        holder.tvCity.setText(bean.getAddress());
        holder.tvProfile.setText(bean.getLdsj());
        holder.tvPhoneCount.setText(bean.getPhoneTimes());
        holder.tvTalkCount.setText(bean.getVisitTimes());
        //条目点击
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CrmDetialActivity.class);
                intent.putExtra(SysConstant.msg_key_parcelable, bean);
                context.startActivity(intent);
            }
        });
    }

    private String getCityName(String address) {
        try {
            if (address != null) {
                if (address.contains("省") & address.contains("市")) {
                    int index_s = address.lastIndexOf("省");
                    int index_e = address.lastIndexOf("市");
                    return address.substring(index_s + 1, index_e + 1);
                }
                if (!address.contains("省") & address.contains("市")) {
                    int index_e = address.lastIndexOf("市");
                    return address.substring(0, index_e + 1);
                }
                return "";
            } else {
                return "";
            }
        } catch (Exception e) {
            Log.e("error", "错误");
            e.printStackTrace();
            return "";
        }


    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        ImageView head;
        TextView headName;
        LinearLayout headParent;
        TextView tvTitle;
        CheckBox checkbox;
        TextView tvState;
        TextView tvCity;
        TextView tvProfile;
        TextView tvPhoneCount;
        TextView tvTalkCount;

        public MyHolder(View itemView) {
            super(itemView);
            head = (ImageView) itemView.findViewById(R.id.head);
            headName = (TextView) itemView.findViewById(R.id.headName);
            headParent = (LinearLayout) itemView.findViewById(R.id.headParent);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
            tvState = (TextView) itemView.findViewById(R.id.tv_state);
            tvCity = (TextView) itemView.findViewById(R.id.tv_city);
            tvProfile = (TextView) itemView.findViewById(R.id.tv_profile);
            tvPhoneCount = (TextView) itemView.findViewById(R.id.tv_phoneCount);
            tvTalkCount = (TextView) itemView.findViewById(R.id.tv_talkCount);
        }
    }


    private interface DelCrmListener {
        void delListener(boolean b, String msg);
    }

}
